import LogFactory from "log/LogFactory";
import {SecurityPath} from "type/SecurityPath";
import {createAsyncThunk, createSlice, PayloadAction} from "@reduxjs/toolkit";
import {requestDelete, requestGet, requestPost, requestPut} from "util/request";
import {Page, PagingData} from "type/Page";
import System from "global/System";
import {StateBaseType} from "../StateBaseType";
import {ReduxStatus} from "enum/ReduxStatus";

const log = LogFactory.getLog("pathReducer") ;

export type PathState = StateBaseType<SecurityPath> ;

const initialState: PathState = {
    data: [] ,
    pageSize: System.getInstance().getUserConfig().pathPageSize,
    current: 1,
    total: 0,
    loading: false,
    status: ReduxStatus.INIT,
} ;
export const fetchPageData = createAsyncThunk(
    'path/fetchPageData',
    async (paging: PagingData) => {
        // log.requestDebug("异步获取用户分页数据", paging) ;
        const response = await requestGet<Page<SecurityPath>>(`/manager/security-path/page/${paging.current}/${paging.pageSize}`) ;
        return response.data ;
    }
) ;
/**
 * 异步添加路径
 */
export const fetchAddData = createAsyncThunk(
    'path/fetchAddData',
    async (path: SecurityPath) => {
        // log.requestDebug("异步获取用户分页数据", paging) ;
        const response = await requestPut<number>("/manager/security-path",path) ;
        return response.data ;
    }
) ;

/**
 * 异步修改路径数据
 */
export const fetchUpdateData = createAsyncThunk(
    'path/fetchUpdateData',
    async (path: SecurityPath) => {
        // log.requestDebug("异步获取用户分页数据", paging) ;
        const response = await requestPost<number>("/manager/security-path",path) ;
        return response.data ;
    }
) ;

export const fetchDeleteData = createAsyncThunk(
    'path/fetchDeleteData',
    async (pathId: number) => {
        const response = await requestDelete<number>("/manager/security-path/" + pathId,{}) ;
        return response.data ;
    }
) ;

export const pathSlice = createSlice({
    name: 'path',
    initialState,
    reducers: {
        deletePath: (state, action: PayloadAction<number>) => {

        }
    },
    extraReducers: builder => {
        builder.addCase(fetchPageData.pending, (state, action) => {
            log.debug("从服务器获取路径分页数据,执行中") ;
            state.loading = true ;
            state.status = ReduxStatus.PENDING ;
        }) ;
        builder.addCase(fetchPageData.fulfilled, (state, action) => {
            log.debug("从服务器获取路径分页数据,执行成功") ;
            state.loading = false ;
            state.status = ReduxStatus.SUCCESS ;
            state.data = action.payload.data ;
            state.current = action.payload.currentPage ;
            state.pageSize = action.payload.pageSize ;
            state.total = action.payload.total ;
        }) ;

        builder.addCase(fetchAddData.pending, (state, action) => {
            log.debug("请求服务器添加路径数据,执行中") ;
            // state.loading = true ;
            // state.status = 1 ;
        });
        builder.addCase(fetchAddData.fulfilled, (state, action) => {
            log.debug("请求服务器添加路径数据,执行成功") ;
            state.loading = true ;
            state.status = ReduxStatus.REFRESH ;
        });

        builder.addCase(fetchUpdateData.fulfilled, (state, action) => {
            log.debug("请求服务器修改路径数据,执行成功") ;
            state.loading = true ;
            state.status = ReduxStatus.REFRESH ;
        });

        builder.addCase(fetchDeleteData.fulfilled, (state, action) => {
            log.debug("请求服务器删除路径数据,执行成功") ;
            state.loading = true ;
            state.status = ReduxStatus.REFRESH ;
        });

    }
}) ;

export const {deletePath} = pathSlice.actions ;
export default pathSlice.reducer ;